Same Tree

Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

题目大意:判断两个二叉树是否相同(形态与节点值相同)

题目难度:Easy

/**
 * Created by gzdaijie on 16/6/7
 */
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if (p == null || q == null) return false;
        return  (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}
gzdaijie            updated 2016-06-07 13:24:24

results matching ""

    No results matching ""